new Vue({
    el: '#app',
    data: {
        selectedStatus: '',
        batchName: '',
        showAddBatchModal: false,
        currentBatch: {
            id: null,
            name: '',
            startTime: '',
            endTime: '',
            description: ''
        },
        batches: [

        ]
    },
    computed: {
        filteredBatches() {
            return this.batches.filter(batch => {
                const matchStatus = this.selectedStatus ? batch.status === this.selectedStatus : true;
                const matchName = this.batchName ? batch.name.includes(this.batchName) : true;
                return matchStatus && matchName;
            });
        }
    },
    methods: {
        getStatusText(status) {
            switch(status) {
                case 'upcoming': return '即将开始';
                case 'ongoing': return '进行中';
                case 'completed': return '已结束';
                default: return '';
            }
        },
        getStatusClass(status) {
            switch(status) {
                case 'upcoming': return 'status-pending';
                case 'ongoing': return 'status-processing';
                case 'completed': return 'status-approved';
                default: return '';
            }
        },
        search() {
            this.currentPage = 1;
        },
        resetFilter() {
            this.selectedStatus = '';
            this.batchName = '';
            this.currentPage = 1;
        },
        viewBatch(id) {
            const batch = this.batches.find(b => b.id === id);
            if (batch) {
                alert(`查看 ${batch.name} 详情：\n开始时间：${batch.startTime}\n结束时间：${batch.endTime}\n状态：${this.getStatusText(batch.status)}\n队伍数量：${batch.teamCount}`);
            }
        },
        editBatch(id) {
            const batch = this.batches.find(b => b.id === id);
            if (batch) {
                this.currentBatch = { ...batch };
                this.showAddBatchModal = true;
            }
        },
        deleteBatch(id) {
            const batch = this.batches.find(b => b.id === id);
            if (batch && confirm(`确定要删除 ${batch.name} 吗？此操作将删除该批次下的所有队伍信息！`)) {
                this.batches = this.batches.filter(b => b.id !== id);
                alert(`已删除 ${batch.name}`);
            }
        },
        closeModal() {
            this.showAddBatchModal = false;
            this.currentBatch = { id: null, name: '', startTime: '', endTime: '', description: '' };
        },
        saveBatch() {
            if (!this.currentBatch.name) {
                alert('请输入批次名称');
                return;
            }
            if (!this.currentBatch.startTime) {
                alert('请选择开始时间');
                return;
            }
            if (!this.currentBatch.endTime) {
                alert('请选择结束时间');
                return;
            }
            if (new Date(this.currentBatch.startTime) > new Date(this.currentBatch.endTime)) {
                alert('开始时间不能晚于结束时间');
                return;
            }

            if (this.currentBatch.id) {
                const index = this.batches.findIndex(b => b.id === this.currentBatch.id);
                if (index !== -1) {
                    this.batches.splice(index, 1, { ...this.currentBatch });
                    alert(`已更新 ${this.currentBatch.name}`);
                }
            } else {
                const newId = Math.max(...this.batches.map(b => b.id), 0) + 1;
                const newBatch = {
                    ...this.currentBatch,
                    id: newId,
                    status: this.getBatchStatus(this.currentBatch.startTime, this.currentBatch.endTime),
                    teamCount: 0,
                    completedSummaryCount: 0
                };
                this.batches.unshift(newBatch);
                alert(`已新增 ${newBatch.name}`);
            }
            this.closeModal();
        },
        getBatchStatus(startTime, endTime) {
            const now = new Date();
            const start = new Date(startTime);
            const end = new Date(endTime);
            if (now < start) return 'upcoming';
            if (now > end) return 'completed';
            return 'ongoing';
        }
    }
});